package com.lanyou.cook.camel.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.xml.ws.Endpoint;

import org.apache.log4j.Logger;

import com.lanyou.cook.camel.service.CamelFtpWebService;
import com.lanyou.cook.camel.service.CamelService;
import com.lanyou.cook.camel.util.ExceptionToStrFactory;

/**
 * webservice 接口实现
 * @author ly-lihailang
 *
 */
public class CamelFtpWebServiceImpl implements CamelFtpWebService {
	private CamelService camelService;
	private static Logger logger = Logger.getLogger(CamelFtpWebServiceImpl.class);

	@Override
	public void process(String methodName, String camelId) {
		logger.info("[CamelFtpWebServiceImpl.process]:methodName="+methodName+"---camelId="+camelId);
		try {
			// 反射获取将要执行的方法
			Method method = camelService.getClass().getDeclaredMethod(
					methodName, String.class);
			// 执行方法
			method.invoke(camelService, camelId);
		} catch (NoSuchMethodException e) {
			logger.error("[CamelFtpWebServiceImpl.process]:"+ExceptionToStrFactory.getErrorInfoFromException(e));
		} catch (SecurityException e) {
			logger.error("[CamelFtpWebServiceImpl.process]:"+ExceptionToStrFactory.getErrorInfoFromException(e));
		} catch (IllegalAccessException e) {
			logger.error("[CamelFtpWebServiceImpl.process]:"+ExceptionToStrFactory.getErrorInfoFromException(e));
		} catch (IllegalArgumentException e) {
			logger.error("[CamelFtpWebServiceImpl.process]:"+ExceptionToStrFactory.getErrorInfoFromException(e));
		} catch (InvocationTargetException e) {
			logger.error("[CamelFtpWebServiceImpl.process]:"+ExceptionToStrFactory.getErrorInfoFromException(e));
		}

	}

	public CamelService getCamelService() {
		return camelService;
	}

	public void setCamelService(CamelService camelService) {
		this.camelService = camelService;
	}

	public static void main(String[] args) {
		System.out.println("web service start");
		CamelFtpWebServiceImpl implementor = new CamelFtpWebServiceImpl();
		String address = "http://localhost:8080/helloWorld";
		Endpoint.publish(address, implementor);
		System.out.println("web service started");
	}

}
